package com.example.qcweb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dao.DBDao;
import com.example.modal.MostPortItem;
import com.example.modal.WorkingProcedure;
import com.example.qc.R;

import java.util.ArrayList;
import java.util.List;

public class PartItemActivity extends Activity {

    private String SystemId, activityname = "PartItemActivity";
    private String controlname = null;
    private Intent myAssiIntent;
    private String operator, rechecker, checker, process, army;

    public class LvButtonOnClickListener implements OnClickListener {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            switch (arg0.getId()) {
                case R.id.tvoperator:
                    if (operator.equals(null)) {

                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();

                        new AlertDialog.Builder(PartItemActivity.this)
                                .setTitle("错误提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();
                        // StartAsignActivity(SystemId);
                    }
                    break;
                case R.id.tvinterchecker:
                    if (operator.equals(null) & rechecker.equals(null)) {

                        controlname = "rechecker";
                        TransferID();
                        StartAsignActivity();

                    } else {
                        new AlertDialog.Builder(PartItemActivity.this)
                                .setTitle("错误提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();

                    }
                    break;
                case R.id.tvchecker:
                    Toast.makeText(PartItemActivity.this, "jianyanyuan",
                            Toast.LENGTH_LONG).show();
                    if (checker.equals(null)) {
                        controlname = "checker";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        new AlertDialog.Builder(PartItemActivity.this)
                                .setTitle("警告提示").setMessage("请检验员刷卡")
                                .setPositiveButton("确定", null).show();

                    }

                    break;

            }
        }

    }

    public class ViewHolder {
        public TextView xuhao;
        public TextView mingcheng;
        public TextView tuhao;
        public TextView lingjianbianhao;
        public TextView canshudaihao;
        public TextView beizhu;
        public TextView caozuozhe;
        public TextView hujianzhe;
        public TextView jianyanyuan;

    }

    WorkingProcedure wpProcedure;
    LayoutInflater inflater;
    List<MostPortItem> mostPortItems = new ArrayList<MostPortItem>();

    public class PartItemAdapter extends BaseAdapter {

        public PartItemAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != mostPortItems) {
                return mostPortItems.size();

            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder = null;
            if (arg1 == null) {
                arg1 = inflater.inflate(R.layout.activity_partitemcell, null);
                viewHolder = new ViewHolder();
                viewHolder.xuhao = (TextView) arg1.findViewById(R.id.tvno);
                viewHolder.mingcheng = (TextView) arg1
                        .findViewById(R.id.tvpartname);
                viewHolder.tuhao = (TextView) arg1.findViewById(R.id.edrecord);
                viewHolder.lingjianbianhao = (TextView) arg1
                        .findViewById(R.id.tvposition);
                viewHolder.canshudaihao = (TextView) arg1
                        .findViewById(R.id.tvcontent);
                viewHolder.beizhu = (TextView) arg1
                        .findViewById(R.id.tvphotono);
                viewHolder.caozuozhe = (TextView) arg1
                        .findViewById(R.id.tvoperator);
                viewHolder.hujianzhe = (TextView) arg1
                        .findViewById(R.id.tvinterchecker);
                viewHolder.jianyanyuan = (TextView) arg1
                        .findViewById(R.id.tvchecker);
                arg1.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) arg1.getTag();
            }
            MostPortItem mPortItem = mostPortItems.get(arg0);
            viewHolder.xuhao.setText(mPortItem.getSystemId());
            viewHolder.mingcheng.setText(mPortItem.getProductName());
            viewHolder.tuhao.setText(mPortItem.getDrawingNumber());
            viewHolder.lingjianbianhao.setText(mPortItem.getProductNumber());
            viewHolder.canshudaihao.setText(mPortItem.getParaCode());
            viewHolder.beizhu.setText(mPortItem.getRemark());
            viewHolder.caozuozhe.setText(mPortItem.getOperator());
            viewHolder.hujianzhe.setText(mPortItem.getReInnerChecker());
            viewHolder.jianyanyuan.setText(mPortItem.getChecker());
            // 给参数赋值
            SystemId = mPortItem.getSystemId();
            operator = mPortItem.getOperator();
            rechecker = mPortItem.getReInnerChecker();
            checker = mPortItem.getChecker();
            Log.i("tag", SystemId + operator + rechecker + checker);

            viewHolder.caozuozhe
                    .setOnClickListener(new LvButtonOnClickListener());
            viewHolder.hujianzhe
                    .setOnClickListener(new LvButtonOnClickListener());
            viewHolder.jianyanyuan
                    .setOnClickListener(new LvButtonOnClickListener());
            return arg1;
        }

    }

    Drawable bgDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_partitem);
        // bgDrawable=getResources().getDrawable(R.drawable.bgpartexammdedia);
        // LinearLayout layout2=(LinearLayout)findViewById(R.id.Layout2);
        // layout2.setBackgroundDrawable(bgDrawable);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        wpProcedure = (WorkingProcedure) bundle.getSerializable("key");
        // setContentView(R.layout.)
        findViews();
        getData();
    }

    private void findViews() {
        ListView lvListView = (ListView) findViewById(R.id.listView1);
        PartItemAdapter adapter = new PartItemAdapter(PartItemActivity.this);
        lvListView.setAdapter(adapter);
        TextView tView = (TextView) findViewById(R.id.wt);
        tView.setText(MyGlobal.KeycardCode + "-工序" + wpProcedure.getProduce()
                + "-工步" + wpProcedure.getStep() + "主要零部件项目表");
        ImageButton partButton = (ImageButton) findViewById(R.id.partBtn);
        partButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(PartItemActivity.this, OperateActivity.class);
                intent.putExtra("type", "PartItem");
                intent.putExtra("key1", wpProcedure);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        bgDrawable = null;

    }

    private void getData() {
        mostPortItems = DBDao.findMostPortItem(wpProcedure.getKEYCardCode(),
                wpProcedure.getSystemId());
    }

    private void TransferID() {
        // TODO Auto-generated method stub
        myAssiIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("systemid", SystemId);
        bundle.putString("activityname", activityname);
        bundle.putString("controlname", controlname);
        myAssiIntent.putExtras(bundle);
        Log.i("tag", controlname);
    }

    private void StartAsignActivity() {
        /* 把bundle对象assign给Intent */
        // Intent asignIntent = new Intent();
        // SystemId=
        myAssiIntent.setClass(PartItemActivity.this, signactivity.class);
        startActivity(myAssiIntent);

    }

}
